package cz.zcu.fav.rat.rest.factory;

import org.springframework.stereotype.Component;

import cz.zcu.fav.rat.beans.ObjectClass;
import cz.zcu.fav.rat.beans.Period;
import cz.zcu.fav.rat.beans.PickAction;
import cz.zcu.fav.rat.beans.ReturnAction;
import cz.zcu.fav.rat.rest.beans.PeriodMapping;
import cz.zcu.fav.rat.rest.utils.RestUtils;

@Component
public class PeriodMappingFactory extends
		AbstractMappingFactory<PeriodMapping, Period>
{

	@Override
	public PeriodMapping getObject()
	{
		return new PeriodMapping();
	}

	@Override
	public Class<?> getObjectType()
	{
		return PeriodMapping.class;
	}

	@Override
	public PeriodMapping getMappedObject(Period period)
	{
		PeriodMapping periodMapping = getObject();
		periodMapping.setFromDate(RestUtils.convertJodaTimeToRestTime(period.getFromDate()));
		periodMapping.setToDate(RestUtils.convertJodaTimeToRestTime(period.getToDate()));
		periodMapping.setId(period.getId());
		periodMapping.setValid(period.isValid());

		
		periodMapping.setPickAction(period.getPickAction());
		periodMapping.setReturnAction(period.getReturnAction());
//		ObjectClass objectClass = period.getReservation().getObject()
//				.getObjectClass();
//		
//		
//		if (objectClass.isPickable())
//		{
//			if (period.isPicked())
//			{
//				periodMapping.setPickAction(PickAction.PICKED);
//			} else
//			{
//				periodMapping.setPickAction(PickAction.NOT_PICKED);
//			}
//		} else
//		{
//			periodMapping.setPickAction(PickAction.NOT_PICKABLE);
//		}
//
//		if (objectClass.isReturnable())
//		{
//			if (period.isReturned())
//			{
//				periodMapping.setReturnAction(ReturnAction.RETURNED);
//			} else
//			{
//				periodMapping
//						.setReturnAction(ReturnAction.NOT_RETURNED);
//			}
//		} else
//		{
//			periodMapping.setReturnAction(ReturnAction.NOT_RETURNABLE);
//		}
		return periodMapping;
	}

	@Override
	public Period getRemappedObject(PeriodMapping object, boolean ignoreId)
	{
		Period period = new Period();
		period.setId(!ignoreId ? object.getId() : null);
		period.setPickAction(object.getPickAction());
		period.setReturnAction(object.getReturnAction());
		period.setValid(object.isValid());
		period.setToDate(RestUtils.convertRestTimeToJodaTime(object.getToDate()));
		period.setFromDate(RestUtils.convertRestTimeToJodaTime(object.getFromDate()));
		return period;
	}

}
